package com.example.sample.logic;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.net.Uri;

/**
 * 
 * ContentProvider演示类，同时演示sqllite的用法
 *  
 * @author  LiangYu
 * @data:  2015-1-5 下午6:22:55 
 * @version:  V1.0
 */
public class RiverContentProvider extends ContentProvider {

	public static class River {
		public River(String name, int len) {
			this.name = name;
			this.len = len;
		}

		public String name;
		public int len;
	}

	public static final String _ID = "_id";
	public static final String NAME = "name";
	public static final String LENGTH = "length";
	public static final String TABLE = "rivers";
	public static final Uri CONTENT_URI = Uri.parse("content://com.example.provider.sample/" + TABLE);

	private static SQLiteDatabase mDb;

	private static final int DB_VER = 2;

	private static final List<River> RIVERS = new ArrayList<River>();

	static {
		River river = new River("长江", 6380);
		RIVERS.add(river);
		river = new River("黄河", 5464);
		RIVERS.add(river);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;

	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues contentValues) {
		return null;
	}

	@Override
	public boolean onCreate() {
		RiverDatabaseHelper helper = new RiverDatabaseHelper(getContext(), TABLE, null, DB_VER);
		mDb = helper.getReadableDatabase();
		return (mDb != null);
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		return mDb.query(TABLE, projection, selection, selectionArgs,
				null, null, sortOrder);
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		return 0;
	}

	private static class RiverDatabaseHelper extends SQLiteOpenHelper {

		public RiverDatabaseHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase database) {
			String sqlStr = "create table " + /*"if not exists " +*/ TABLE
					+ "( _id integer primary key autoincrement," + " name text,"
					+ "length integer" + ");";
			database.execSQL(sqlStr);
			SQLiteStatement statement = database
					.compileStatement("insert into "+TABLE+"(name,length) values(?,?)");

			for (River r : RIVERS) {
				int index = 1;
				statement.bindString(index++, r.name);
				statement.bindLong(index++, r.len);
				statement.executeInsert();
			}
			statement.close();
		}

		@Override
		public void onUpgrade(SQLiteDatabase database, int oldVersion,
				int newVersion) {
			database.execSQL("drop table if exists " + TABLE);
			onCreate(database);
		}
	}
}